/**
 * API调用示例
 * 本文件提供了如何在页面中使用API服务层的示例代码
 */

// 导入API
import { getOverdueBillList, getPayableBillList, searchBills } from './overdue.js';
import { cacheRequest } from '../utils/cache.js';
import { parseOverdueDaysRange, parseAmountRange } from '../utils/index.js';

/**
 * 逾期账单列表页示例
 */
export const overdueBillExample = {
  data() {
    return {
      billList: [],
      isLoading: false,
      noMore: false,
      page: 1,
      pageSize: 10,
      // 筛选参数
      filterParams: {
        department: '',
        billType: 'all',
        overdueDays: '',
        amountRange: ''
      }
    };
  },
  methods: {
    // 处理筛选变化
    handleFilterChange(params) {
      console.log('Filter changed:', params);
      
      // 更新筛选参数
      if (params.department) {
        this.filterParams.department = params.department;
      }
      if (params.billType) {
        this.filterParams.billType = params.billType;
      }
      if (params.filter && params.filter.length) {
        params.filter.forEach(item => {
          if (item.type === 'overdueDays') {
            this.filterParams.overdueDays = item.value;
          } else if (item.type === 'amountRange') {
            this.filterParams.amountRange = item.value;
          }
        });
      }
      
      // 重置分页并加载数据
      this.page = 1;
      this.billList = [];
      this.noMore = false;
      this.loadBillList();
    },
    
    // 加载账单列表
    async loadBillList() {
      if (this.isLoading) return;
      this.isLoading = true;
      
      try {
        // 构建请求参数
        const params = {
          page: this.page,
          pageSize: this.pageSize,
          ...this.filterParams
        };
        
        // 发起API请求
        const result = await getOverdueBillList(params);
        
        // 处理响应数据
        if (result && result.data) {
          if (this.page === 1) {
            this.billList = result.data;
          } else {
            this.billList = [...this.billList, ...result.data];
          }
          
          // 判断是否还有更多数据
          this.noMore = result.data.length < this.pageSize;
        } else {
          this.noMore = true;
        }
      } catch (error) {
        console.error('加载账单列表失败:', error);
        uni.showToast({
          title: '加载失败，请重试',
          icon: 'none'
        });
      } finally {
        this.isLoading = false;
      }
    },
    
    // 加载更多
    loadMore() {
      if (this.noMore || this.isLoading) return;
      this.page++;
      this.loadBillList();
    },
    
    // 使用缓存请求示例
    async loadBillListWithCache() {
      if (this.isLoading) return;
      this.isLoading = true;
      
      try {
        // 构建请求参数
        const params = {
          page: this.page,
          pageSize: this.pageSize,
          ...this.filterParams
        };
        
        // 生成缓存键
        const cacheKey = `overdue_bills_${JSON.stringify(params)}`;
        
        // 使用缓存请求
        const result = await cacheRequest(
          cacheKey,
          () => getOverdueBillList(params),
          1000 * 60 * 5 // 5分钟缓存
        );
        
        // 处理响应数据
        if (result && result.data) {
          if (this.page === 1) {
            this.billList = result.data;
          } else {
            this.billList = [...this.billList, ...result.data];
          }
          
          // 判断是否还有更多数据
          this.noMore = result.data.length < this.pageSize;
        } else {
          this.noMore = true;
        }
      } catch (error) {
        console.error('加载账单列表失败:', error);
        uni.showToast({
          title: '加载失败，请重试',
          icon: 'none'
        });
      } finally {
        this.isLoading = false;
      }
    }
  }
};

/**
 * 待付账单列表页示例
 */
export const payableBillExample = {
  // 类似于overdueBillExample，但使用getPayableBillList API
};

/**
 * 搜索页示例
 */
export const searchBillsExample = {
  data() {
    return {
      keyword: '',
      searchResults: [],
      isLoading: false
    };
  },
  methods: {
    // 搜索账单
    async searchBills() {
      if (!this.keyword.trim()) {
        uni.showToast({
          title: '请输入搜索关键词',
          icon: 'none'
        });
        return;
      }
      
      this.isLoading = true;
      
      try {
        const result = await searchBills({
          keyword: this.keyword.trim()
        });
        
        if (result && result.data) {
          this.searchResults = result.data;
        } else {
          this.searchResults = [];
        }
      } catch (error) {
        console.error('搜索失败:', error);
        uni.showToast({
          title: '搜索失败，请重试',
          icon: 'none'
        });
      } finally {
        this.isLoading = false;
      }
    }
  }
};